/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufc.quixada.adrs.dao;

import br.ufc.quixada.adrs.dao.config.PostGresMapConfig;
import br.ufc.quixada.adrs.model.Endereco;
import java.sql.SQLException;
import java.util.List;

/**
 *
 * @author Caio
 */
public class EnderecoDAO {

    public void insert(Endereco endereco) throws SQLException {
        Long id = getProxId();
        endereco.setId(id);
        PostGresMapConfig.getSqlMapClient().insert("addEndereco", endereco);
    }

    public void update(Endereco endereco) throws SQLException {
        PostGresMapConfig.getSqlMapClient().update("updateEndereco", endereco);
    }

    public void delete(Long id) throws SQLException {
        PostGresMapConfig.getSqlMapClient().delete("deleteEndereco", id);
    }

    public Endereco getById(Long id) throws SQLException {
        return (Endereco) PostGresMapConfig.getSqlMapClient().queryForObject("getEnderecoById", id);
    }

    public List<Endereco> getByEventoId(Long id) throws SQLException {
        return (List<Endereco>) PostGresMapConfig.getSqlMapClient().queryForList("getEnderecosByEventoId", id);
    }
 public List<Endereco> getAll() throws SQLException {
        return (List<Endereco>) PostGresMapConfig.getSqlMapClient().queryForList("getAllEnderecos");
    }
    private Long getProxId() throws SQLException {
        Long id = (Long) PostGresMapConfig.getSqlMapClient().queryForObject("getMaxIdEndereco");
        if (id == null) {
            id = 0L;
        }
        return id + 1L;
    }
}
